package com.bestcem.xm.user.grpc.client;

import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.dingtalkapp.grpc.v1.services.DingtalkAppServiceGrpc;
import com.bestcem.xm.dingtalkapp.grpc.v1.services.GetDingSessionDataRequest;
import com.bestcem.xm.dingtalkapp.grpc.v1.services.GetDingSessionDataResponse;
import com.bestcem.xm.user.grpc.client.dto.DingSessionDataDTO;
import com.zy.dp.proto.base.v1.Code;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

/**
 * 钉钉相关grpc接口
 *
 * @author panyun <yun.pan@idiaoyan.com>
 * @version v1.0
 * @date 2022/7/13 16:51
 */
@Slf4j
@Service(value = "bpDingTalkGrpcClientService")
public class BpDingTalkGrpcClientService {

    @GrpcClient(GrpcClientNames.XM)
    private DingtalkAppServiceGrpc.DingtalkAppServiceBlockingStub stub;

    public DingSessionDataDTO getDingSessionData(String sessionId) {
        GetDingSessionDataRequest.Builder builder = GetDingSessionDataRequest.newBuilder();
        builder.setSessionId(sessionId);

        GetDingSessionDataResponse response = stub.getDingSessionData(builder.build());

        // 校验查询结果
        if (Code.ERROR_VALUE == response.getStatus().getCodeValue()) {
            log.error("获取钉钉缓存数据失败：sessionId:{} ", sessionId);
            return null;
        }

        DingSessionDataDTO dto = new DingSessionDataDTO();
        dto.setDingUserId(response.getUserId())
                .setDingUnionId(response.getUnionId())
                .setName(response.getName());
        return dto;
    }

}
